<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
  
<script>
import echarts from "echarts"
import resize from "@/framework/utils/mixins/resize"

require("echarts/theme/macarons")

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    options: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    })
  },
  watch: {
    options: {
      handler(options) {
        this.chart.setOption(options, true);
      },
      deep: true,
    },
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons")
      this.chart.setOption(this.options, true)
      this.chart.resize()
    }
  },
}
</script>
  